/**   
 * Copyright © 2019 eSunny Info. Tech Ltd. All rights reserved.
 * 
 * @Description:TODO(功能)
 * @className LoginServlet.java
 * @Package: com.bus.servlet 
 * @author: yj   
 * @date: 2019年1月26日 下午12:00:56 
 */
package com.erp.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.erp.po.Admin;
import com.erp.service.AdminServiceImpl;

/**
 * @className LoginServlet.java
 * @author yj
 * @Description:TODO(功能)
 * @Date 2019年1月26日
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {

		String name = request.getParameter("userName");
		String pwd = request.getParameter("password");
		String time = request.getParameter("freeSecret");
		
		System.out.println(name+"  "+pwd);
		
		AdminServiceImpl as = new AdminServiceImpl();
		Admin admin = as.findAdminByLogin(new Admin(name, pwd));
		
		HttpSession session = request.getSession();
		if (admin != null) {
			
			ServletContext app = request.getServletContext();
			HashMap<String, HttpSession> userMap = (HashMap<String, HttpSession>) app.getAttribute("userMap");
			
			System.out.println(userMap);
			
			if(userMap != null) {
				
				if(userMap.containsKey(admin.getAdminName())) {
					 HttpSession loginSession = userMap.get(admin.getAdminName());
					 userMap.remove(admin.getAdminName());
					 loginSession.removeAttribute("loginName");
					 
					 userMap.put(admin.getAdminName(), session);
				} else {
					userMap.put(admin.getAdminName(), session);
				}
				
			} else {
				// 第一次登陆.把值写到一个map中
				HashMap<String, HttpSession> map = new HashMap<String,HttpSession>();
				map.put(admin.getAdminName(), session);
				app.setAttribute("userMap", map);
			}
			
			// 维持长时间登录状态, 就是设置存储JSESSIONID 的 cookie的时长
			Cookie[] arr =request.getCookies();
			if(arr!=null) {
				for(Cookie c:arr) {
					if("JSESSIONID".equals(c.getName())) {
						if("0".equals(time)) {
							
						} else if("1".equals(time)) {
							c.setMaxAge(60*60*24);
						} else if("7".equals(time)) {
							c.setMaxAge(60*60*24*7);
						} else if("30".equals(time)) {
							c.setMaxAge(60*60*24*30);
						}
						response.addCookie(c);
					}
				}
			}
			
			session.setAttribute("loginName", admin.getAdminName());
			response.sendRedirect("main.jsp");
		} else {
			PrintWriter p = response.getWriter();
			//p.write("<script>alert('sorry , your userName or password was wrong ! Please refill.');location.href = 'index.html';</script>");
			p.write("<script>alert('对不起,你的用户名或者密码错误!请重新输入.');location.href = 'index.jsp';</script>");
		}
		
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException {
		doGet(req, resp);
	}

}
